clear all
set printcolor gs1, permanently
set more off
global dirfigures ""
global dirgeolocalized ""
global dircenso ""
global dirmatlab ""


import delimited "$dirmatlab/matches_o_trips_manzanas.csv", encoding(ISO-8859-1) clear
save "$dirmatlab/matches_o_trips_manzanas.dta", replace

import delimited "$dirmatlab/manzanas_DF_dist_to_EDOMEX.csv", encoding(ISO-8859-1) clear
rename id census_tract_id
rename id_closest_df census_tract_id_closestinedomex
keep census_tract_id dist_to_edomex census_tract_id_closestinedomex
save "$dirgeolocalized/manzanas_DF_dist_to_EDOMEX.dta", replace

import delimited "$dirmatlab/manzanas_EDOMEX_dist_to_DF.csv", encoding(ISO-8859-1) clear
rename id census_tract_id
rename id_closest_df census_tract_id_closestindf
keep census_tract_id dist_to_df census_tract_id_closestindf
save "$dirgeolocalized/manzanas_EDOMEX_dist_to_DF.dta", replace

use "$dirgeolocalized/Datos Geolocalizados.dta", clear
egen long id=group(trip_uuid)
rename id trip_id
duplicates report trip_id
format trip_id %15.0g
save "$dirgeolocalized/Datos Geolocalizados.dta", replace

import delimited "$dirmatlab/matches_d_trips_manzanas.csv", encoding(ISO-8859-1) clear
save "$dirmatlab/matches_d_trips_manzanas.dta", replace
use "$dirmatlab/matches_d_trips_manzanas.dta", clear
rename census_tract_id ID
merge m:1 ID using "$dircenso/censo_CDMX_geo.dta", nogenerate keep(match) keepusing(CVE_ENT)
rename ID census_tract_idmerge m:1 census_tract_id using "$dirgeolocalized/manzanas_DF_dist_to_EDOMEX.dta"
drop if _merge==2
drop _merge
merge m:1 census_tract_id using "$dirgeolocalized/manzanas_EDOMEX_dist_to_DF.dta"
drop if _merge==2
drop _merge
save "$dirmatlab/matches_d_trips_manzanas_ALL.dta", replace


use "$dirmatlab/matches_o_trips_manzanas.dta", clear
rename census_tract_id ID
merge m:1 ID using "$dircenso/censo_CDMX_geo.dta", nogenerate keep(match) keepusing(CVE_ENT)
rename ID census_tract_id
merge m:1 census_tract_id using "$dirgeolocalized/manzanas_DF_dist_to_EDOMEX.dta"
drop if _merge==2
drop _merge
merge m:1 census_tract_id using "$dirgeolocalized/manzanas_EDOMEX_dist_to_DF.dta"
drop if _merge==2
drop _merge
save "$dirmatlab/matches_o_trips_manzanas_ALL.dta", replace

use "$dirgeolocalized/Datos Geolocalizados.dta", clear
format id %15.0g
drop trip_uuid client_uuid dropoff_lat dropoff_lng id_old
gen date=substr(request,1,10)
split date, p("-")
rename date1 year
drop request_timestamp_local date*
destring year, replace
rename id trip_id
merge 1:1 trip_id using "$dirmatlab/matches_o_trips_manzanas_ALL.dta", keep(match) nogenerate

geoinpoly begintrip_lat begintrip_lng using "$dirgeolocalized/Cash_EDOMEX_geofence-polygon_cor.dta"

gen type=.
replace type=1 if _ID==1 & CVE_ENT=="15"
replace type=2 if _ID==1 & CVE_ENT=="09"
replace type=3 if _ID==. & CVE_ENT=="15"
replace type=4 if _ID==. & CVE_ENT=="09"
replace census_tract_id=census_tract_id_closestindf if type==3
replace census_tract_id=census_tract_id_closestinedomex if type==2

drop if distance_mts>500  
gen trips=1
collapse (sum) trips original_fare_usd (mean) mean_distance_mts=distance_mts (max) max_distance_mts=distance_mts, by(census_tract_id year is_cash_trip )
gen cash=(is_cash_trip=="TRUE")
drop is_cash_trip

reshape wide original_fare_usd trips mean_distance_mts max_distance_mts,i(year census_tract_id) j(cash)
rename original_fare_usd0 fares_card
rename original_fare_usd1 fares_cash
rename trips0 trips_card
rename trips1 trips_cash
rename mean_distance_mts0 mean_distance_mts_card
rename mean_distance_mts1 mean_distance_mts_cash
rename max_distance_mts0 max_distance_mts_card
rename max_distance_mts1 max_distance_mts_cash

merge m:1 census_tract_id using "$dirgeolocalized/manzanas_DF_dist_to_EDOMEX.dta", keepusing(dist_to_edomex) 
drop if _merge==2
drop _merge

merge m:1 census_tract_id using "$dirgeolocalized/manzanas_EDOMEX_dist_to_DF.dta", keepusing(dist_to_df) 
drop if _merge==2
drop _merge

rename census_tract_id ID
merge m:1 ID using "$dircenso/censo_CDMX_geo.dta", nogenerate keep(match)

save "$dirgeolocalized/collapsed_uber_censo_lessthan500mts.dta", replace

keep year ID trips_card fares_card trips_cash fares_cash dist_to_edomex dist_to_df id ///
CVE_ENT NOM_ENT CVE_MUN NOM_MUN CVE_LOC NOM_LOC CVE_AGEB CVE_MZA x_centroid y_centroid ///
POBTOT VIVTOT REL_H_M PROM_HNV GRAPROES  PNACENT P3YM_HLI PHOG_IND PCON_LIM PEA PEA_M ///
PEA_F P12YM_CASA VPH_PISOTI VPH_1DOR VPH_1CUART VPH_S_ELEC VPH_AGUAFV VPH_NODREN VPH_EXCSA  ///
VPH_SNBIEN VPH_AUTOM VPH_CEL VPH_INTER 


export delimited year ID trips_card fares_card trips_cash fares_cash dist_to_edomex dist_to_df id ///
CVE_ENT NOM_ENT CVE_MUN NOM_MUN CVE_LOC NOM_LOC CVE_AGEB CVE_MZA x_centroid y_centroid ///
POBTOT VIVTOT REL_H_M PROM_HNV GRAPROES  PNACENT P3YM_HLI PHOG_IND PCON_LIM PEA PEA_M ///
PEA_F P12YM_CASA VPH_PISOTI VPH_1DOR VPH_1CUART VPH_S_ELEC VPH_AGUAFV VPH_NODREN VPH_EXCSA  ///
VPH_SNBIEN VPH_AUTOM VPH_CEL VPH_INTER using "$dirmatlab/collapsed_uber_censo_lessthan500mts.csv", replace


use "$dirgeolocalized/collapsed_uber_censo_lessthan500mts.dta", clear
keep year ID trips_card fares_card trips_cash fares_cash dist_to_edomex dist_to_df id ///
CVE_ENT NOM_ENT CVE_MUN NOM_MUN CVE_LOC NOM_LOC CVE_AGEB CVE_MZA x_centroid y_centroid ///
POBTOT POBFEM VIVTOT REL_H_M PROM_HNV GRAPROES  PNACENT P3YM_HLI PHOG_IND PCON_LIM PEA PEA_M ///
PEA_F P12YM_CASA VPH_PISOTI VPH_1DOR VPH_1CUART VPH_S_ELEC VPH_AGUAFV VPH_NODREN VPH_EXCSA  ///
VPH_SNBIEN VPH_AUTOM VPH_CEL VPH_INTER GRAPROES_M GRAPROES_F max_distance_mts* mean_distance_mts*

drop if year==2018
gen share_female=POBFEM/POBTOT
egen trips=rowtotal(trips_card trips_cash)
egen fares=rowtotal(fares_card fares_cash)
replace trips_cash=0 if trips_cash==.
replace fares_cash=0 if fares_cash==.
gen share_cash=fares_cash/fares

keep if POBTOT>0
keep if VIVTOT>0

gen distance=dist_to_df
replace distance=-dist_to_edomex if CVE_ENT=="09"
replace distance=distance/1000

foreach var in PNACENT P3YM_HLI PHOG_IND PCON_LIM PEA PEA_M PEA_F P12YM_CASA {
gen s`var'=`var'/POBTOT
}

foreach var in VPH_PISOTI VPH_1DOR VPH_1CUART VPH_S_ELEC VPH_AGUAFV VPH_NODREN VPH_EXCSA VPH_SNBIEN VPH_AUTOM VPH_CEL VPH_INTER {
gen s`var'=`var'/VIVTOT
}

binscatter share_cash GRAPROES if GRAPROES>5 & GRAPROES<16 & year==2017 & CVE_ENT=="15", ///
nq(100) linetype(none) lcolor(navy) mcolor(ebblue) xtitle(Average years of schooling) ///
ytitle(Share of trips paid in cash) savegraph("$dirfigures/Figure5a.eps") replace  

binscatter share_cash sVPH_INTER [w=VIVTOT] if year==2017 & CVE_ENT=="15", ///
nq(100) linetype(none) lcolor(navy) mcolor(ebblue) ///
xtitle("Share of homes with internet") ///
ytitle(Share of trips paid in cash) savegraph("$dirfigures/Figure5b.eps") replace
	
binscatter share_cash sVPH_CEL [w=VIVTOT] if year==2017 & CVE_ENT=="15", ///
nq(100) linetype(none) lcolor(navy) mcolor(ebblue) ///
xtitle("Share of homes with cell phone") ///
ytitle(Share of trips paid in cash) savegraph("$dirfigures/Figure5c.eps") replace
		
	
binscatter share_cash sVPH_AUTOM [w=VIVTOT] if year==2017 & CVE_ENT=="15", ///
nq(100) linetype(none) lcolor(navy) mcolor(ebblue) ///
xtitle("Share of homes with car") ///
ytitle(Share of trips paid in cash) savegraph("$dirfigures/Figure5d.eps") replace
		
pca GRAPROES sVPH_CEL sVPH_INTER sVPH_AUTOM
predict PCA1
binscatter share_cash PCA1 if year==2017 & CVE_ENT=="15", ///
nq(100) linetype(none) lcolor(navy) mcolor(ebblue) xtitle(Principal component scores for component 1) ///
ytitle(Share of trips paid in cash) savegraph("$dirfigures/FigureC3b.eps") replace	
		
		
 
tsset ID year
tsfill, full

foreach var in trips fares trips_card fares_card {
replace `var'=0 if `var'==.
}

capture drop d_trips* d_fares*
gen d_trips=2*(trips-l.trips)/(trips+l.trips)
gen d_fares=2*(fares-l.fares)/(fares+l.fares)
gen d_trips_card=2*(trips_card-l.trips_card)/(trips_card+l.trips_card)
gen d_fares_card=2*(fares_card-l.fares_card)/(fares_card+l.fares_card)
gen d_trips_real=(-2-d_trips)/(d_trips-2) -1



#delimit;
global outcomes 
d_trips
;
#delimit cr

global margins distance

local c=15
local bw=1

preserve
keep if year==2017
gen bins_distance=. 
		forvalues k=1/`c' {
			local vrmin=0+(`k'-1)*`bw'
			local vrmax=0+`k'*`bw'
			local vlmin=0-(`k'-1)*`bw'
			local vlmax=0-(`k')*`bw'
			replace bins_distance=`vrmax' if distance>=`vrmin' & distance<`vrmax'
			replace bins_distance=`vlmax' if distance>=`vlmax' & distance<`vlmin' 	
					}
		gen distanceD=1 if distance>0 
		replace distanceD=0 if distance<0 
	
by bins_distance, sort : egen d_tripsA_distance=mean(d_trips)	
			
			set graphics off
			qui lpoly d_trips distance if distanceD == 0 & abs(distance)<15, degree(3) kernel(epanechnikov) generate(x0 s0) se(se0) 
			qui lpoly d_trips distance if distanceD == 1 & abs(distance)<15, degree(3) kernel(epanechnikov) generate(x1 s1) se(se1) 
	
			for num 0 1: ge luX = sX + 2.57*seX 
			for num 0 1: ge llX = sX - 2.57*seX 
	
			set graphics on
			
			#delimit;
			scatter d_tripsA_distance bins_distance if abs(distance)<15, leg(off) mcolor(ebblue) || 
			line s0 lu0 ll0 x0 if abs(distance)<15, leg(off) lpattern(solid shortdash shortdash) lcolor(navy navy navy) lwidth(medthick medium medium)  || 
			line s1 lu1 ll1 x1 if abs(distance)<15,  lpattern(solid shortdash shortdash) lcolor(navy navy navy)  
			ytitle(Growth in trips) xtitle(Kilometers to Mexico City)   ylabel(.4[.4]1.6) 
			graphregion(fcolor(white) lcolor(white)) xline(0, lcolor(black)) xlabel(-15[5]15)	
			;
			#delimit cr
			graph export "$dirfigures/Figure6b.eps", replace	
restore



			
#delimit;
global outcomes 
GRAPROES  sVPH_AUTOM sVPH_CEL sVPH_INTER
;
#delimit cr
global margins distance
		

local c=15
local bw=1
keep if year==2017
keep if abs(distance)<15
gen bins_distance=. 
		forvalues k=1/`c' {
			local vrmin=0+(`k'-1)*`bw'
			local vrmax=0+`k'*`bw'
			local vlmin=0-(`k'-1)*`bw'
			local vlmax=0-(`k')*`bw'
			replace bins_distance=`vrmax' if distance>=`vrmin' & distance<`vrmax'
			replace bins_distance=`vlmax' if distance>=`vlmax' & distance<`vlmin' 	
					}
		gen distanceD=1 if distance>0 
		replace distanceD=0 if distance<0 
		
	foreach y in $outcomes {
		by bins_distance, sort : egen `y'A_distance=mean(`y')
					}
										
capture drop x0 s0 se0 x1 s1 se1 lu0 lu1 ll0 ll1
set graphics off
qui lpoly GRAPROES distance if distanceD == 0 & abs(distance)<15, degree(3) kernel(epanechnikov) generate(x0 s0) se(se0) 
qui lpoly GRAPROES distance if distanceD == 1 & abs(distance)<15, degree(3) kernel(epanechnikov) generate(x1 s1) se(se1) 
for num 0 1: ge luX = sX + 2.57*seX 
for num 0 1: ge llX = sX - 2.57*seX 
set graphics on
			
			#delimit;
			scatter GRAPROESA_distance bins_distance if abs(distance)<15, leg(off) mcolor(ebblue) || 
			line s0 lu0 ll0 x0 if abs(distance)<15, leg(off) lpattern(solid shortdash shortdash) lcolor(navy navy navy) lwidth(medthick medium medium) || 
			line s1 lu1 ll1 x1 if abs(distance)<15,  lpattern(solid shortdash shortdash) lcolor(navy navy navy)  
			ytitle(Average years of schooling) xtitle(Kilometers to Mexico City) /* ylabel(0[.4]1.6) */
			graphregion(fcolor(white) lcolor(white)) xline(0, lcolor(black)) xlabel(-15[5]15)	
			;
			#delimit cr
			graph export "$dirfigures/FigureC9a.eps", replace
			
capture drop x0 s0 se0 x1 s1 se1 lu0 lu1 ll0 ll1
set graphics off
qui lpoly sVPH_AUTOM distance if distanceD == 0 & abs(distance)<15, degree(3) kernel(epanechnikov) generate(x0 s0) se(se0) 
qui lpoly sVPH_AUTOM distance if distanceD == 1 & abs(distance)<15, degree(3) kernel(epanechnikov) generate(x1 s1) se(se1) 
for num 0 1: ge luX = sX + 2.57*seX 
for num 0 1: ge llX = sX - 2.57*seX 
set graphics on
			
			#delimit;
			scatter sVPH_AUTOMA_distance bins_distance if abs(distance)<15, leg(off) mcolor(ebblue) || 
			line s0 lu0 ll0 x0 if abs(distance)<15, leg(off) lpattern(solid shortdash shortdash) lcolor(navy navy navy) lwidth(medthick medium medium) || 
			line s1 lu1 ll1 x1 if abs(distance)<15,  lpattern(solid shortdash shortdash) lcolor(navy navy navy)  
			ytitle(Share of homes with car) xtitle(Kilometers to Mexico City) /* ylabel(0[.4]1.6) */
			graphregion(fcolor(white) lcolor(white)) xline(0, lcolor(black)) xlabel(-15[5]15)	
			;
			#delimit cr
			graph export "$dirfigures/FigureC9b.eps", replace
			
capture drop x0 s0 se0 x1 s1 se1 lu0 lu1 ll0 ll1
set graphics off
qui lpoly sVPH_CEL distance if distanceD == 0 & abs(distance)<15, degree(3) kernel(epanechnikov) generate(x0 s0) se(se0) 
qui lpoly sVPH_CEL distance if distanceD == 1 & abs(distance)<15, degree(3) kernel(epanechnikov) generate(x1 s1) se(se1) 
for num 0 1: ge luX = sX + 2.57*seX 
for num 0 1: ge llX = sX - 2.57*seX 
set graphics on
			
			#delimit;
			scatter sVPH_CELA_distance bins_distance if abs(distance)<15, leg(off) mcolor(ebblue) || 
			line s0 lu0 ll0 x0 if abs(distance)<15, leg(off) lpattern(solid shortdash shortdash) lcolor(navy navy navy) lwidth(medthick medium medium) || 
			line s1 lu1 ll1 x1 if abs(distance)<15,  lpattern(solid shortdash shortdash) lcolor(navy navy navy)  
			ytitle(Share of homes with cell phone) xtitle(Kilometers to Mexico City) /* ylabel(0[.4]1.6) */
			graphregion(fcolor(white) lcolor(white)) xline(0, lcolor(black)) xlabel(-15[5]15)	
			;
			#delimit cr
			graph export "$dirfigures/FigureC9c.eps", replace
			
			
capture drop x0 s0 se0 x1 s1 se1 lu0 lu1 ll0 ll1 
set graphics off
qui lpoly sVPH_INTER distance if distanceD == 0 & abs(distance)<15, degree(3) kernel(epanechnikov) generate(x0 s0) se(se0) 
qui lpoly sVPH_INTER distance if distanceD == 1 & abs(distance)<15, degree(3) kernel(epanechnikov) generate(x1 s1) se(se1) 
for num 0 1: ge luX = sX + 2.57*seX 
for num 0 1: ge llX = sX - 2.57*seX 
set graphics on
			
			#delimit;
			scatter sVPH_INTERA_distance bins_distance if abs(distance)<15, leg(off) mcolor(ebblue) || 
			line s0 lu0 ll0 x0 if abs(distance)<15, leg(off) lpattern(solid shortdash shortdash) lcolor(navy navy navy) lwidth(medthick medium medium) || 
			line s1 lu1 ll1 x1 if abs(distance)<15,  lpattern(solid shortdash shortdash) lcolor(navy navy navy)  
			ytitle(Share of homes with internet) xtitle(Kilometers to Mexico City) /* ylabel(0[.4]1.6) */
			graphregion(fcolor(white) lcolor(white)) xline(0, lcolor(black)) xlabel(-15[5]15)	
			;
			#delimit cr
			graph export "$dirfigures/FigureC9d.eps", replace

	
use "$dirgeolocalized/entrydate_CDMX.dta", clear
gen timing_org=.
replace timing=1 if entrydate>20331 & entrydate<=20695
replace timing=2 if entrydate>20695 & entrydate<=21059
replace timing=3 if entrydate>21059 & entrydate<=21423
replace timing=0 if entrydate<=20331
replace timing=4 if timing==. 
save "$dirgeolocalized/TEMP_entrydate_CDMX.dta", replace

gen new_users=1
merge 1:1 client using "$dirgeolocalized/users_geolocalized.dta", nogenerate keep(match)
collapse (sum) new_users count, by(ID_origin timing_org)
tsset ID_origin timing_org
tsfill, full 
foreach var in new_users {
replace `var'=0 if `var'==.
}

capture drop d_new_users
gen d_new_users=2*(new_users-l.new_users)/(new_users+l.new_users)

rename ID_origin census_tract_id
merge m:1 census_tract_id using "$dirgeolocalized/manzanas_DF_dist_to_EDOMEX.dta", keepusing(dist_to_edomex) 
drop if _merge==2
drop _merge

merge m:1 census_tract_id using "$dirgeolocalized/manzanas_EDOMEX_dist_to_DF.dta", keepusing(dist_to_df) 
drop if _merge==2
drop _merge

gen distance=dist_to_df
replace distance=-dist_to_edomex if distance==.
replace distance=distance/1000
drop dist_to_df dist_to_edomex

rename census_tract_id ID
merge m:1 ID using "$dircenso/censo_CDMX_geo.dta", nogenerate keep(match)
save "$dirgeolocalized/collapsed_uber_censo_extensive.dta", replace


use "$dirgeolocalized/entrydate_CDMX.dta", clear
gen timing_org=.
replace timing=1 if entrydate>20331 & entrydate<=20695
replace timing=2 if entrydate>20695 & entrydate<=21059
replace timing=3 if entrydate>21059 & entrydate<=21423
replace timing=0 if entrydate<=20331
replace timing=4 if timing==. 
save "$dirgeolocalized/TEMP_entrydate_CDMX.dta", replace

gen new_users=1
merge 1:1 client using "$dirgeolocalized/users_geolocalized_ver2.dta", nogenerate keep(match)
collapse (sum) new_users count, by(ID_origin timing_org)
tsset ID_origin timing_org
tsfill, full 
foreach var in new_users {
replace `var'=0 if `var'==.
}

capture drop d_new_users
gen d_new_users=2*(new_users-l.new_users)/(new_users+l.new_users)

rename ID_origin census_tract_id
merge m:1 census_tract_id using "$dirgeolocalized/manzanas_DF_dist_to_EDOMEX.dta", keepusing(dist_to_edomex) 
drop if _merge==2
drop _merge

merge m:1 census_tract_id using "$dirgeolocalized/manzanas_EDOMEX_dist_to_DF.dta", keepusing(dist_to_df) 
drop if _merge==2
drop _merge

gen distance=dist_to_df
replace distance=-dist_to_edomex if distance==.
replace distance=distance/1000
drop dist_to_df dist_to_edomex

rename census_tract_id ID
merge m:1 ID using "$dircenso/censo_CDMX_geo.dta", nogenerate keep(match)
save "$dirgeolocalized/collapsed_uber_censo_extensive_ver2.dta", replace

 

use "$dirgeolocalized/collapsed_uber_censo_extensive.dta", clear
 

#delimit;
global outcomes 
d_new_users
;
#delimit cr
global margins distance

local c=15
local bw=1

preserve
keep if timing_org==2
gen bins_distance=. 
		forvalues k=1/`c' {
			local vrmin=0+(`k'-1)*`bw'
			local vrmax=0+`k'*`bw'
			local vlmin=0-(`k'-1)*`bw'
			local vlmax=0-(`k')*`bw'
			replace bins_distance=`vrmax' if distance>=`vrmin' & distance<`vrmax'
			replace bins_distance=`vlmax' if distance>=`vlmax' & distance<`vlmin' 	
					}
		gen distanceD=1 if distance>0 
		replace distanceD=0 if distance<0 
	
by bins_distance, sort : egen d_new_usersA_distance=mean(d_new_users)	
			
			set graphics off
			qui lpoly d_new_users distance if distanceD == 0 & abs(distance)<15, degree(3) kernel(epanechnikov) generate(x0 s0) se(se0) 
			qui lpoly d_new_users distance if distanceD == 1 & abs(distance)<15, degree(3) kernel(epanechnikov) generate(x1 s1) se(se1) 
	
			for num 0 1: ge luX = sX + 2.57*seX 
			for num 0 1: ge llX = sX - 2.57*seX 
	
			set graphics on
			
			#delimit;
			scatter d_new_usersA_distance bins_distance if abs(distance)<15, leg(off) mcolor(ebblue) || 
			line s0 lu0 ll0 x0 if abs(distance)<15, leg(off) lpattern(solid shortdash shortdash) lcolor(navy navy navy) lwidth(medthick medium medium) || 
			line s1 lu1 ll1 x1 if abs(distance)<15,  lpattern(solid shortdash shortdash) lcolor(navy navy navy)  
			ytitle(Growth in users) xtitle(Kilometers to Mexico City) ylabel(0[.4]1.6) 
			graphregion(fcolor(white) lcolor(white)) xline(0, lcolor(black)) xlabel(-15[5]15)	
			;
			#delimit cr
			graph export "$dirfigures/Figure6a.eps", replace
restore		

 

 
use "$dirgeolocalized/collapsed_uber_censo_lessthan500mts.dta", clear
keep year ID trips_card fares_card trips_cash fares_cash dist_to_edomex dist_to_df id ///
CVE_ENT NOM_ENT CVE_MUN NOM_MUN CVE_LOC NOM_LOC CVE_AGEB CVE_MZA x_centroid y_centroid ///
POBTOT VIVTOT REL_H_M PROM_HNV GRAPROES PNACOE PNACENT P3YM_HLI PHOG_IND PCON_LIM PEA PEA_M ///
PEA_F P12YM_CASA VPH_PISOTI VPH_1DOR VPH_1CUART VPH_S_ELEC VPH_AGUAFV VPH_NODREN VPH_EXCSA  ///
VPH_SNBIEN VPH_AUTOM VPH_CEL VPH_INTER GRAPROES_M GRAPROES_F max_distance_mts* mean_distance_mts*

drop if year==2017
egen year1=group(year)
drop year
rename year1 year

egen trips=rowtotal(trips_card trips_cash)
egen fares=rowtotal(fares_card fares_cash)
gen share_cash=fares_cash/fares

keep if POBTOT>0
keep if VIVTOT>0

gen distance=dist_to_df
replace distance=-dist_to_edomex if CVE_ENT=="09"
replace distance=distance/1000
	
tsset ID year
tsfill, full

foreach var in trips fares {
replace `var'=0 if `var'==.
}


capture drop d_trips d_fares
gen d_trips=2*(trips-l.trips)/(trips+l.trips)
gen d_fares=2*(fares-l.fares)/(fares+l.fares)

 
#delimit;
global outcomes 
d_trips
;
#delimit cr

global margins distance

local c=15
local bw=1

preserve
keep if year==2
gen bins_distance=. 
		forvalues k=1/`c' {
			local vrmin=0+(`k'-1)*`bw'
			local vrmax=0+`k'*`bw'
			local vlmin=0-(`k'-1)*`bw'
			local vlmax=0-(`k')*`bw'
			replace bins_distance=`vrmax' if distance>=`vrmin' & distance<`vrmax'
			replace bins_distance=`vlmax' if distance>=`vlmax' & distance<`vlmin' 	
					}
		gen distanceD=1 if distance>0 
		replace distanceD=0 if distance<0 
	
by bins_distance, sort : egen d_tripsA_distance=mean(d_trips)	
			
			set graphics off
			qui lpoly d_trips distance if distanceD == 0 & abs(distance)<15, degree(3) kernel(epanechnikov) generate(x0 s0) se(se0) 
			qui lpoly d_trips distance if distanceD == 1 & abs(distance)<15, degree(3) kernel(epanechnikov) generate(x1 s1) se(se1) 
	
			for num 0 1: ge luX = sX + 2.57*seX 
			for num 0 1: ge llX = sX - 2.57*seX 
	
			set graphics on
			
			#delimit;
			scatter d_tripsA_distance bins_distance if abs(distance)<15, leg(off) mcolor(ebblue) || 
			line s0 lu0 ll0 x0 if abs(distance)<15, leg(off) lpattern(solid shortdash shortdash) lcolor(navy navy navy) lwidth(medthick medium medium) || 
			line s1 lu1 ll1 x1 if abs(distance)<15,  lpattern(solid shortdash shortdash) lcolor(navy navy navy)  
			ytitle(Growth in trips) xtitle(Kilometers to Mexico City) ylabel(.4[.4]1.6) 
			graphregion(fcolor(white) lcolor(white)) xline(0, lcolor(black)) xlabel(-15[5]15)	
			;
			#delimit cr
			graph export "$dirfigures/Figure6c.eps", replace
			
restore


use "$dirgeolocalized/collapsed_uber_censo_lessthan500mts.dta", clear
keep year ID trips_card fares_card trips_cash fares_cash dist_to_edomex dist_to_df id ///
CVE_ENT NOM_ENT CVE_MUN NOM_MUN CVE_LOC NOM_LOC CVE_AGEB CVE_MZA x_centroid y_centroid ///
POBTOT VIVTOT REL_H_M PROM_HNV GRAPROES  PNACENT P3YM_HLI PHOG_IND PCON_LIM PEA PEA_M ///
PEA_F P12YM_CASA VPH_PISOTI VPH_1DOR VPH_1CUART VPH_S_ELEC VPH_AGUAFV VPH_NODREN VPH_EXCSA  ///
VPH_SNBIEN VPH_AUTOM VPH_CEL VPH_INTER GRAPROES_M GRAPROES_F max_distance_mts* mean_distance_mts*

drop if year==2016
egen year1=group(year)
drop year
rename year1 year

egen trips=rowtotal(trips_card trips_cash)
egen fares=rowtotal(fares_card fares_cash)
gen share_cash=fares_cash/fares

keep if POBTOT>0
keep if VIVTOT>0

gen distance=dist_to_df
replace distance=-dist_to_edomex if CVE_ENT=="09"
replace distance=distance/1000
		  	
tsset ID year
tsfill, full

foreach var in trips fares trips_card fares_card {
replace `var'=0 if `var'==.
}


capture drop d_trips* d_fares*
gen d_trips=2*(trips-l.trips)/(trips+l.trips)
gen d_fares=2*(fares-l.fares)/(fares+l.fares)
gen d_trips_card=2*(trips_card-l.trips_card)/(trips_card+l.trips_card)
gen d_fares_card=2*(fares_card-l.fares_card)/(fares_card+l.fares_card)
 		
#delimit;
global outcomes 
d_trips
;
#delimit cr

global margins distance

local c=15
local bw=1

preserve
keep if year==2
gen bins_distance=. 
		forvalues k=1/`c' {
			local vrmin=0+(`k'-1)*`bw'
			local vrmax=0+`k'*`bw'
			local vlmin=0-(`k'-1)*`bw'
			local vlmax=0-(`k')*`bw'
			replace bins_distance=`vrmax' if distance>=`vrmin' & distance<`vrmax'
			replace bins_distance=`vlmax' if distance>=`vlmax' & distance<`vlmin' 	
					}
		gen distanceD=1 if distance>0 
		replace distanceD=0 if distance<0 
	
by bins_distance, sort : egen d_tripsA_distance=mean(d_trips)	
			
			set graphics off
			qui lpoly d_trips distance if distanceD == 0 & abs(distance)<15, degree(3) kernel(epanechnikov) generate(x0 s0) se(se0) 
			qui lpoly d_trips distance if distanceD == 1 & abs(distance)<15, degree(3) kernel(epanechnikov) generate(x1 s1) se(se1) 
	
			for num 0 1: ge luX = sX + 2.57*seX 
			for num 0 1: ge llX = sX - 2.57*seX 
	
			set graphics on
			
			#delimit;
			scatter d_tripsA_distance bins_distance if abs(distance)<15, leg(off) mcolor(ebblue) || 
			line s0 lu0 ll0 x0 if abs(distance)<15, leg(off) lpattern(solid shortdash shortdash) lcolor(navy navy navy) lwidth(medthick medium medium) || 
			line s1 lu1 ll1 x1 if abs(distance)<15,  lpattern(solid shortdash shortdash) lcolor(navy navy navy)  
			ytitle(Growth in trips) xtitle(Kilometers to Mexico City) ylabel(0[.4]1.6) 
			graphregion(fcolor(white) lcolor(white)) xline(0, lcolor(black)) xlabel(-15[5]15)	
			;
			#delimit cr
			graph export "$dirfigures/Figure6d.eps", replace
			
restore


		
